package k5_net.demo04;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class PictureDownload {

    public static void main(String[] args) {
        try {
            //1.通过url地址，封装成一个url对象
            URL url = new URL("http://gips2.baidu.com/it/u=195724436,3554684702&fm=3028&app=3028&f=JPEG&fmt=auto?w=1280&h=960");

            //2.打开连接
            URLConnection connection = url.openConnection();

            //3.通过连接获取内容的字节输入流
            InputStream inputStream = connection.getInputStream();

            //4.创建一个缓冲字节输出流
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E://a.jpg"));

            //5.由输入流将内容复制到输出流中
            inputStream.transferTo(bos);

            //6.关闭连接
            bos.close();
            inputStream.close();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
